package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.UserFansQuery;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TravelMapper travelMapper;

    @Override
    public void saveOrCheckEmail(User user) {
        //判断邮箱是否存在,存在就跑出异常,否则就保存作为新的用户
        User temp = userMapper.queryForEmailAndPassword(user.getEmail(), null);
        if (temp == null) {
            userMapper.insert(user);
        } else {
            throw new RuntimeException("邮箱已注册");
        }
    }

    @Override
    public User login(String email, String password) {
        User user = userMapper.queryForEmailAndPassword(email, password);
          if(user==null){
              throw new RuntimeException("账号和密码不匹配,请重试");
          }
        return user;
    }
    @Override
    public PageInfo<?> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<User> list = userMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void update(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public User getUserByAuthorId(Long authorId) {
        return userMapper.getUserByAuthorId(authorId);
    }

    @Override
    public void updateFans(User user) {
        User fan = UserContext.getCurrentUser();
       if(fan==null){
           return;
       }
        Map map = userMapper.selectFanByUserId(user.getId(),fan.getId());
        if(map==null){
        Long id = user.getId();
        User userd = userMapper.selectByPrimaryKey(id);
        user.setFans(userd.getFans()+1);
        user.setFollow(userd.getFollow());
        userMapper.userFansAndFollow(user);

        //关注人数的保存
            fan.setFans(fan.getFans());
            fan.setFollow(fan.getFollow()+1);
        userMapper.userFansAndFollow(fan);
        userMapper.insertFans(user.getId(),fan.getId());
       }else{
            throw new RuntimeException("已注册");
        }
    }

    @Override
    public void deleteFans(Long id) {
        User fan = UserContext.getCurrentUser();
        User userd = userMapper.selectByPrimaryKey(id);
        userd.setFans(userd.getFans()-1);
        userd.setFollow(userd.getFollow());
        userMapper.userFansAndFollow(userd);
        //取消关注的发起人
        fan.setFans(fan.getFans());
        fan.setFollow(fan.getFollow()-1);
        userMapper.userFansAndFollow(fan);
        userMapper.deleteRelation(userd.getId(),fan.getId());
    }

    //查询所有用户
    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    public User selectIntegralById(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveLike(Travel travel) {
        Long currentUserId = UserContext.getCurrentUser().getId();
        HashMap map=userMapper.selectLike(currentUserId,travel.getId());
        if(map==null){
            userMapper.saveLike(currentUserId,travel.getId(),travelMapper.selectByPrimaryKey(travel.getId()).getAuthor().getId());
             travelMapper.selectByPrimaryKey(travel.getId());
            travel.setLikeCount(travelMapper.selectByPrimaryKey(travel.getId()).getLikeCount()+1);
            travelMapper.updateLike(travel);
        }else{
            throw new RuntimeException("已经点赞");
        }
    }

    @Override
    public HashMap queryLike(Long id) {
       return userMapper.selectLike(UserContext.getCurrentUser().getId(),id);
    }

    @Override
    public void deleteLike(Long travelId) {
        userMapper.deleteLike(UserContext.getCurrentUser().getId(),travelId);
        Travel travel = travelMapper.selectByPrimaryKey(travelId);
        System.out.println(travel);
        travel.setLikeCount(travel.getLikeCount()-1);
        travelMapper.updateLike(travel);
    }

    @Override
    public HashMap queryFans(Long id) {
        return userMapper.selectFanByUserId(id,UserContext.getCurrentUser().getId());
    }

    @Override
    public List<User> listAllFansOrFollow(UserFansQuery qo) {
        return userMapper.selectAllFansOrfollows(qo);
    }
}
